package com.example.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.R;
import com.example.entity.Capture;
import com.example.entity.Const;
import com.example.module.dialog.LoginDialog;
import com.example.module.dialog.LoginLayout;
import com.example.persistence.MyApplication;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.DisplayImageOptions.Builder;
import com.nostra13.universalimageloader.core.ImageLoader;

public class SplashActivity extends Activity {
	protected static final String TAG = "SplashActivity";
	private ImageView preloadImage;
	private MyApplication application;
	private FrameLayout splashLayout;

	@Override
	public void onCreate(Bundle savedInstanceState) {
	    super.onCreate(savedInstanceState);
	    setContentView(R.layout.activity_splash);
	    
	    application = (MyApplication) getApplication();
	    
	    splashLayout = (FrameLayout) findViewById(R.id.splashLayout);
	    preloadImage = (ImageView) findViewById(R.id.preloadImage);
	    
	    AsyncTask<String, Integer, Integer> preloader = new AsyncTask<String, Integer, Integer>(){

	    	@Override
	    	protected void onPreExecute() {
	    		super.onPreExecute();
	    	}
	    	
	    	@Override
	    	protected void onProgressUpdate(Integer... values) {
	    		super.onProgressUpdate(values);
	    	}
	    	
			@Override
			protected Integer doInBackground(String... params) {
				application.loadData();
				
				Log.e(TAG, "captureList.size()=" + application.getCaptureList().size());
				
				for (int i = 0; application.getCaptureList() != null && i < application.getCaptureList().size(); i++) {
					Builder builder = new DisplayImageOptions.Builder();
					if (i < Const.IMAGE_MEMORY_CACHE_COUNT)
						builder.cacheInMemory();
					
					DisplayImageOptions options = builder.cacheOnDisc().build();
					
					Capture capture = application.getCaptureList().get(i);
					
					String file = capture.getThumbnail();
					if (file == null || file.length() < 1) file = capture.getPath(); 
					final int index = i;
					
					Log.d(TAG, index + ") capture : " + file);
					ImageLoader.getInstance().preloadImage(file, preloadImage, options);
				}
				
				return null;
			}
			
			@Override
			protected void onPostExecute(Integer result) {
				super.onPostExecute(result);
				
				
				LoginDialog dialog = new LoginDialog(SplashActivity.this, new LoginLayout.LoginListener() {
					
					@Override
					public void OnSuccess() {
						goMainActivity();	
					}
					
					@Override
					public void OnFail() {
						Toast.makeText(SplashActivity.this, "Login Fail", Toast.LENGTH_SHORT).show();
					}
				});
				
				
				dialog.show();
				//goMainActivity();
			}
	    };
	    
	    preloader.execute();
	}
	
	protected void goMainActivity() {
    	Intent newIntent = new Intent(this, MainActivity.class);
    	newIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(newIntent);
        finish();
    }
}
